/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui.security;

import java.awt.Dimension;
import java.io.IOException;
import java.sql.SQLException;

import blueprint4j.db.DBConnection;
import blueprint4j.db.DataException;
import blueprint4j.db.Entity;
import blueprint4j.gui.Binder;
import blueprint4j.gui.DataGroup;
import blueprint4j.gui.DataPanel;
import blueprint4j.gui.DataPanelInterface;
import blueprint4j.gui.db.DefaultFieldBinder;
import blueprint4j.gui.db.EntityEditor;
import blueprint4j.gui.db.EntityPanelFieldBinder;
import blueprint4j.utils.BindException;
import blueprint4j.utils.Bindable;


public class PanelUser extends EntityEditor {
	private User user;
	private DBConnection connection = null;
	private Entity limit_entity = null;

	public PanelUser(DBConnection connection,Entity limit_entity)
	throws DataException, SQLException {
		super(connection,"Users");
		this.connection = connection;
		this.limit_entity = limit_entity;
		try {
			//if (limit_entity != null) {
			//	user=new User(connection,limit_entity.id.get());
			//} else {
			user=new User(connection);
			//}
			setDetails(
			    user,
			    new EntityPanelFieldBinder[]{
			        new DefaultFieldBinder(user.username,150)
			    },
			    new DataPanelInterface[]{
			        new DataPanelUser(connection,limit_entity)
			    }
			);
			enableSearch(false);
			setListPreferredSize(new Dimension(150, 300));
			// BUILD A SQL SEACH STRING TO
			setOtherWhere(User.CURRENT_USER.getSQLForSubPrivilege(limit_entity));
			// only an administrator or a user with a limit id can create new limitations
			if (limit_entity != null && !User.CURRENT_USER.isAdmin() &&
			        (User.CURRENT_USER.limit_id.get() == null ||
			        !User.CURRENT_USER.limit_id.get().equals(limit_entity.id.getAsString()))) {
				enableAdd(false);
			}
		} catch (BindException be) {
			throw new DataException(be);
		}
	}

	public static class DataPanelUser extends DataPanel {

		private DBConnection connection = null;
		private Entity limit_entity = null;

		public DataPanelUser(DBConnection connection,Entity limit_entity) {
			super();
			this.connection = connection;
			this.limit_entity = limit_entity;
		}

		public String getName() {
			return "User";
		}

		public void build(Binder binder, Bindable entity)
		throws IOException, BindException {
			try{
				User user = (User)entity;
				if (!user.isLoaded()) {
					if (limit_entity != null) {
						user.limit_id.set(limit_entity.id.getAsString());
					} else {
						user.limit_id.set(User.CURRENT_USER.limit_id.get());
					}
				}
				binder.addBindable(user);
				DataGroup type = new DataGroup("User information");
				type.add(binder.createDataPair(user.username,true));
				type.add(binder.createDataPair(user.passwd));
				type.add(binder.createDataPair(user.first_name,true));
				type.add(binder.createDataPair(user.last_name,true));
				type.add(binder.createDataPair(user.email,true));
				type.add(binder.createDataPairFieldStringCombo(user.privilege,User.CURRENT_USER.getSubPrivilegeDisplay().toArray(),User.CURRENT_USER.getSubPrivilege().toArray(),true));
				addGroup(type);
			} catch (Exception exception) {
				throw new BindException(exception);
			}
		}

		public void activated() {}
		public void deactivated() {}
	}
}

